<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 订单管理
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{

    /**
     * Order模型对象
     * @var \app\admin\model\Order
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Order;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            // 状态搜索
            $filter = $this->request->get("filter", '');
            $where = json_decode($filter, true) ?? [];

            if(isset($where['state']) && $where['state'] == 0){
                unset($where['state']);
                $where['order.paid'] = 1;$where['order.status'] = 0;
            }
            if(isset($where['state']) && $where['state'] == 1){
                unset($where['state']);
                $where['order.paid'] = 1;$where['order.status'] = ['IN', '1,2,3,4'];
            }
            if(isset($where['state']) && $where['state'] == 2){
                unset($where['state']);
                $where['order.paid'] = 1;$where['order.status'] = 5;
            }
            if(isset($where['state']) && $where['state'] == 7){
                unset($where['state']);
                $where['order.status'] = ['IN', '100,101,102,103'];
            }
            if(isset($where['state']) && $where['state'] == -1){
                unset($where['state']);
                $where['order.paid'] = 0;$where['order.status'] = 0;
            }
            if(isset($where['createtime'])){
                $v = str_replace(' - ', ',', $where['createtime']);
                $arr = array_slice(explode(',', $v), 0, 2);
                $where['order.createtime'] = ['BETWEEN TIME' , [$arr[0],$arr[1]]];
            }

            $list = $this->model
                    ->with(['user','steward','package'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);
            foreach ($list as $row) {
                $row->visible(['id','order_no','student_name','school_addr','home_addr','pay_price','pay_type','status','createtime']);
                $row->visible(['user']);
				$row->getRelation('user')->visible(['nickname']);
				$row->visible(['steward']);
				$row->getRelation('steward')->visible(['realname']);
				$row->visible(['package']);
				$row->getRelation('package')->visible(['title']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 详情
     */
    public function details($ids = null)
    {
        $row = $this->model->get($ids);

        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if($row['user_id']){
            $row['user'] = model('app\admin\model\User')
                ->where(['id' => $row['user_id']])
                ->field('id,nickname')
                ->find();
        }else{
            $row['user']['nickname'] = '用户';
        }

        if($row['coupon_id']){
            $row['coupon'] = model('app\admin\model\Coupon')
                ->where(['id' => $row['coupon_id']])
                ->field('id,name')
                ->find();
        }

        $row['service'] = model('app\admin\model\ServicePackage')
            ->where(['id' => $row['service_id']])
            ->field('id,title,price,image')
            ->find();

        $row['extra'] = model('app\admin\model\OrderExtra')
            ->where(['order_id' => $row['id'],'user_id'=>$row['user_id'],'paid'=>1])
            ->field('id,order_no,title,duration,price,pay_type')
            ->select();

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

}
